package com.gopay.cron.service;

import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.domain.cust.CustUniqIdenRel;
import com.gopay.common.exception.GopayException;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.user.dao.CustPersonInfoQueryDAO;
import com.gopay.common.util.StringTools;
import com.gopay.cron.dao.trans.CustUniqIdenRelDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CLASSNAME:CustUniqIdenRelServiceImpl
 * Description:
 * DATE:2018/6/13/013
 * AUTHER:PANSHICHAO
 * www.gopay.com.cn Inc.All rights reserved.
 */
@Service("custUniqIdenService")
public class CustUniqIdenRelServiceImpl implements CustUniqIdenRelService{
    @Autowired
    private CustUniqIdenRelDao custUniqIdenRelDao;
    @Autowired
    private CommonQueryDAO commonQueryDAO;
    @Resource(name="custPersonInfoQueryDao")
    private CustPersonInfoQueryDAO custPersonInfoQueryDao;
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {
            Exception.class, RuntimeException.class })
    public void saveCustuniqIdenRelInfo(CustUniqIdenRel rel) throws GopayException {
        custUniqIdenRelDao.save(rel);
    }

    @Override
    public Integer findByCustIdAndCertNo(String certNo, String custId) {
        String sql = "select CUST_NAME,UNIQ_IDEN from CUST_UNIQ_IDEN_REL where id_number=:certNo and CUST_ID = :custId order by REGIS_TS";
        Map<String,Object> param = new HashMap<String,Object>(2);
        param.put("certNo",certNo);
        param.put("custId",custId);
        List<CustUniqIdenRel> results =  custUniqIdenRelDao.findBySql(sql,param);
        if(results !=null && !results.isEmpty()){
            return results.size();
        }else{
            return 0;
        }
    }

    @Override
    public String showUniqIndeInfoByCertNoAndCustId(String certNo, String custId) {
        String sql = "select CUST_NAME,UNIQ_IDEN from CUST_UNIQ_IDEN_REL where id_number=:certNo and CUST_ID = :custId order by REGIS_TS";
        Map<String,Object> param = new HashMap<String,Object>(2);
        param.put("certNo",certNo);
        param.put("custId",custId);
        List<Object[]> results =  commonQueryDAO.findBySql(sql,param);
        if(results !=null && !results.isEmpty()){
            return StringTools.checkNulltoString(results.get(0)[1]);
        }else{
            return "";
        }
    }

    @Override
    public List<CustPersonInfo> showAllIsnotRealNameCertifyToday() {
        String hql = "from CustPersonInfo where isRealNameCertify= 2  and createdTs >trunc(:startDate) and createdTs <trunc(:endDate)";
        Map<String,Object> param = new HashMap<String,Object>(2);
        Date nowDate = new Date();
        Long nowDateMillions = System.currentTimeMillis();
        Long nextDateMillions = nowDateMillions+24*3600*1000;
        Date nextDate = new Date(nextDateMillions);
        param.put("startDate",nowDate);
        param.put("endDate",nextDate);
        List<CustPersonInfo> resultList = custPersonInfoQueryDao.findByHql(hql,param);
        if(resultList.isEmpty()){
            return null;
        }else{
            return resultList;
        }
    }
}
